home *** CD-ROM | disk | FTP | other *** search
- /*
- * FILE: testapp.c
- * AUTHOR: R. Gonzalez
- * CREATED: August 25, 1990
- *
- * Defines simple "database" application commands, main function.
- *
- * ASSOCIATED FILES:
- * app.c,app.h,class.c,class.h,comapp.c,comapp.h,comline.c,
- * comline.h,macapp.c,macapp.h,menapp.c,menapp.h,menu.c,menu.h,
- * menubar.c,menubar.h,morestr.c,morestr.h,dfile.c,dfile.h,
- * testapp.h.
- *
- * PROJECT CONTENTS (Macintosh):
- * above-listed source (.c) files, MacTraps, ANSI or ANSI-881,
- * oops library.
- *
- * COMPILATION (Macintosh):
- * include precompiled header MacHeaders, 68881 code generation
- * if ANSI-881 is used.
- */
-
- # ifdef THINK_C
- # include <oops.h>
- # endif
-
- # include <string.h>
- # include "testapp.h"
-
- # define MAX_LENGTH 80
-
- static boolean newfil(Test_App*);
- static boolean oopen(Test_App*);
- static boolean save(Test_App*);
- static boolean quit(Test_App*);
- static boolean enter_data(Test_App*);
- static boolean list_data(Test_App*);
- static boolean sort_by_name(Test_App*);
- static boolean sort_by_age(Test_App*);
- static boolean report_filename(Test_App*);
-
- /************************************************************************
- * initialize application
- ************************************************************************/
- boolean Test_App::init(void)
- {
- PARENT_CLASS::init(); /* don't forget to call this! */
-
- menu->log_command(1,0,"File",NULL);
- menu->log_command(1,1,"New",(comfunc) newfil);
- menu->log_command(1,2,"Open",(comfunc) oopen);
- menu->log_command(1,3,"Save",(comfunc) save);
- menu->log_command(1,4,"Quit",(comfunc) quit);
- menu->log_command(2,0,"Data",NULL);
- menu->log_command(2,1,"Enter",(comfunc) enter_data);
- menu->log_command(2,2,"List",(comfunc) list_data);
- menu->log_command(3,0,"Options",NULL);
- menu->log_command(3,1,"SortByName",(comfunc) sort_by_name);
- menu->log_command(3,2,"SortByAge",(comfunc) sort_by_age);
- menu->log_command(3,3,"ReportFilename",(comfunc) report_filename);
-
- menu->log_command(1,5,"Bye",(comfunc) quit);
- menu->log_command(1,6,"Goodbye",(comfunc) quit);
- menu->log_command(1,7,"Stop",(comfunc) quit);
- menu->log_command(1,8,"Logout",(comfunc) quit);
- menu->log_command(1,9,"Logoff",(comfunc) quit);
- menu->log_command(1,10,"Exit",(comfunc) quit);
- menu->log_command(1,11,"Ciao",(comfunc) quit);
- menu->log_command(1,12,"AufWiedersehen",(comfunc) quit);
- menu->log_command(1,13,"AuRevoir",(comfunc) quit);
- menu->log_command(1,14,"SeeYouLaterAlligator",(comfunc) quit);
-
- dfile = new(Dfile);
- dfile->init();
-
- return TRUE;
- }
-
- /************************************************************************
- * terminate application - free up allocated space.
- ************************************************************************/
- boolean Test_App::destroy(void)
- {
- dfile->destroy();
- delete(dfile);
-
- return PARENT_CLASS::destroy();
- }
-
- /*------------------------ command functions --------------------------*/
-
- /************************************************************************
- * clear database memory.
- ************************************************************************/
- static boolean newfil(Test_App *app_ptr)
- {
- app_ptr->dfile->destroy();
- app_ptr->dfile->init();
-
- return TRUE;
- }
-
- /************************************************************************
- * open record file. For some reason there was a conflict with the
- * name "open" so I used "oopen".
- ************************************************************************/
- static boolean oopen(Test_App *app_ptr)
- {
- char temp_char[MAX_LENGTH];
-
- app_ptr->query("Enter file name: ",temp_char);
-
- if (!app_ptr->dfile->open_existing(temp_char))
- app_ptr->respond("No such file.");
-
- return TRUE;
- }
-
- /************************************************************************
- * save records.
- ************************************************************************/
- static boolean save(Test_App *app_ptr)
- {
- char temp_char[MAX_LENGTH];
-
- app_ptr->query("Enter file name (RETURN for previous): ",temp_char);
-
- if (!app_ptr->dfile->write_valid(temp_char))
- app_ptr->respond("Can't open file.");
-
- return TRUE;
- }
-
- /************************************************************************
- * quit.
- ************************************************************************/
- boolean quit(Test_App *app_ptr)
- {
- app_ptr->done = TRUE;
- return TRUE;
- }
-
- /************************************************************************
- * enter data record.
- ************************************************************************/
- static boolean enter_data(Test_App *app_ptr)
- {
- boolean success;
- char *question1 = "Enter your name: ",
- *question2 = "Enter your age: ",
- answer1[MAX_LENGTH],
- answer2[MAX_LENGTH];
-
- app_ptr->query(question1,answer1);
- app_ptr->query(question2,answer2);
-
- if (strcmp(answer1,"") != 0 || strcmp(answer2,"") != 0)
- {
- success = TRUE;
- if (!app_ptr->dfile->add_record(answer1,answer2))
- app_ptr->respond("Too many records.");
- }
- else
- success = FALSE;
-
- return success;
- }
-
- /************************************************************************
- * list data records.
- ************************************************************************/
- static boolean list_data(Test_App *app_ptr)
- {
- int i=0;
- char response[MAX_LENGTH];
-
- while (app_ptr->dfile->get_record(i++,response))
- app_ptr->respond(response);
-
- if (i == 1)
- app_ptr->respond("No entries.");
-
- return TRUE;
- }
-
- /************************************************************************
- * sort by name.
- ************************************************************************/
- static boolean sort_by_name(Test_App *app_ptr)
- {
- return app_ptr->dfile->sort_by_name();
- }
-
- /************************************************************************
- * sort by age.
- ************************************************************************/
- static boolean sort_by_age(Test_App *app_ptr)
- {
- return app_ptr->dfile->sort_by_age();
- }
-
- /************************************************************************
- * report filename. This is just included to demonstrate how to access
- * query() and respond() from other objects.
- ************************************************************************/
- static boolean report_filename(Test_App *app_ptr)
- {
- app_ptr->dfile->show_name(app_ptr);
-
- return TRUE;
- }
-
- /************************************************************************
- * main - you found it!
- ************************************************************************/
- main()
- {
- Generic_App *application;
-
- application = new(Test_App);
- application->init();
- application->run();
- application->destroy();
- delete(application);
- }
-
-